﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEVN.Web.Framework.Controls
{
    public partial class ControlPanel : System.Web.UI.UserControl
    {
        WebFrameDataContext db = new WebFrameDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cboPages.DataSource = db.MyPages;
                cboPages.DataBind();

                cboSkins.DataSource = db.Skins;
                cboSkins.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        protected void cboSkins_SelectedIndexChanged(object sender, EventArgs e)
        {
            Skin sk = db.Skins.SingleOrDefault(s => s.SkinID.ToString() == cboSkins.SelectedValue);

            if (sk != null)
            {
                pnlSkin.Visible = true;
                imgSkin.ImageUrl = "~/" + sk.Image;                
            }          
        }

        protected void cboPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);
            if (pg != null)
            {
                ScriptManager.RegisterStartupScript(this.Page, GetType(), "loadDefault", "<script type='text/javascript'>loadDynamic('~/"+cboPages.SelectedItem.Text+"')</script>",true);
                //ClientScript.RegisterStartupScript(GetType(), "loadDefault", "<script type='text/javascript'>loadDynamic()</script>");
                List<MyControl> listControl = new List<MyControl>();
                if (pg.Position != null)
                {
                    foreach (MyControl ct in db.MyControls)
                    {
                        bool isAdd = false;
                        foreach (char ch in pg.Position)
                        {
                            bool check = false;
                            foreach (char c in ct.Position)
                            {
                                if (ch == c)
                                {
                                    check = true;
                                    break;
                                }
                            }

                            if (check)
                            {
                                isAdd = true;
                                break;
                            }
                        }

                        if (isAdd)
                            listControl.Add(ct);
                    }

                }

                cboControls.DataSource = listControl;
                cboControls.DataBind();
            }
        }      

        protected void chkSelected_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSelected.Checked)
            {
                pnlSelectSkin.Enabled = true;
            }
            else
            {
                pnlSelectSkin.Enabled = false;
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);
            if (pg != null)
            {
                pg.SkinID = int.Parse(cboSkins.SelectedValue);
                db.SubmitChanges();

                Response.Redirect(Request.Url.ToString());

                chkSelected.Checked = false;
                pnlSelectSkin.Enabled = false;
                chkAccept.Checked = false;
            }
        }

        protected void chkAccept_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAccept.Checked)
                btnApply.Enabled = true;
            else
                btnApply.Enabled = false;
        }

        protected void lblCloseSkin_Click(object sender, EventArgs e)
        {

        }
    }
}